﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using SystemControllers;

namespace eTMS.Forms.Reviews
{
    public partial class FrmBangDiemToanKhoa : Form
    {
        public FrmBangDiemToanKhoa()
        {
            InitializeComponent();
        }
        SinhVienController controller = new SinhVienController();
        private void FrmBangDiemToanKhoa_Load(object sender, EventArgs e)
        {
            sinhVienInfoInput1.SetController(controller);
        }

        private void btnCreateScoreTable_Click(object sender, EventArgs e)
        {
            HocKyController hocKyController = new HocKyController();
            ChuongTrinhDaoTaoController ctrlChuongTrinh = new ChuongTrinhDaoTaoController();
            ChuongTrinhDaoTaoController.LoadAllChuongTrinhDaoTao(false);
            string boChuQuan = CommonController.GetStringConfig("REPORT_TOP");
            string truong = CommonController.GetStringConfig("REPORT_SUBTOP");
            controller.ResetObject();
            sinhVienInfoInput1.UpdateToModel();
            controller.SearchSinhVien((int)treeKhoaNganhKhoaHoc1.SelectedKhoa_ID, treeKhoaNganhKhoaHoc1.SelectedNganhHoc_ID, treeKhoaNganhKhoaHoc1.SelectedLop_ID, treeKhoaNganhKhoaHoc1.KhoaHoc_ID);
            DSBangDiemToanKhoa ds = new DSBangDiemToanKhoa();
            foreach (DataRow r in controller.Table.Rows)
            {
                DSBangDiemToanKhoa.tblBangDiemRow row = ds.tblBangDiem.NewtblBangDiemRow();
                row.BOCHUQUAN = boChuQuan;
                row.HEDAOTAO = HeDaoTaoController.HeDaoTao.TenHeDaoTao;
                row.HOTEN = r["TenSinhVien"].ToString();
                row.MASV = r["MaSinhVien"].ToString();
                row.NGAYSINH = r["NgaySinh"].ToString();
                row.NGANHHOC = r["TENNGANHHOC"].ToString();
                row.SINHVIEN_ID = controller.ConvertInt(r["SINHVIEN_ID"]);
                row.TENTRUONG = truong;
                ds.tblBangDiem.Rows.Add(row);
            }
            DataTable tblMaster = controller.Table;
            controller.SearchSinhVienWithAllDiemMonHocForScoreTable(treeKhoaNganhKhoaHoc1.SelectedKhoa_ID, treeKhoaNganhKhoaHoc1.SelectedNganhHoc_ID, treeKhoaNganhKhoaHoc1.SelectedLop_ID, treeKhoaNganhKhoaHoc1.KhoaHoc_ID, controller.GetListSinhVien_ID());
            int currentNganh = 0;
            int currentKhoaHoc = 0;
            int maxRow=10;
            int countNamThuHocKy = 0;
            string keyNamThuHocKy = "";
            foreach (DataRow r in tblMaster.Rows)
            {
                if (controller.ConvertInt(r["NganhHoc_ID"]) != currentNganh || currentKhoaHoc != controller.ConvertInt(r["KhoaHoc_ID"]))
                {
                    currentNganh = controller.ConvertInt(r["NganhHoc_ID"]);
                    currentKhoaHoc = controller.ConvertInt(r["KhoaHoc_ID"]);
                    ctrlChuongTrinh.LoadChuongTrinhDaoTaoSuDungByKhoaNganh(currentKhoaHoc, currentNganh);
                    ctrlChuongTrinh.LoadChiTiet();
                    ctrlChuongTrinh.TableChiTiet.DefaultView.Sort = "NamThu,HocKy_ID";
                    ctrlChuongTrinh.TableChiTiet = ctrlChuongTrinh.TableChiTiet.DefaultView.ToTable();
                    countNamThuHocKy = 0;
                    List<string> lsNamThuHocKy = new List<string>();
                    for (int i = 0; i < ctrlChuongTrinh.TableChiTiet.Rows.Count; i++)
                    {
                        string key = ctrlChuongTrinh.TableChiTiet.Rows[i]["NamThu"].ToString() + "_" + ctrlChuongTrinh.TableChiTiet.Rows[i]["HocKy_ID"].ToString();
                        if (!lsNamThuHocKy.Contains(key))
                        {
                            countNamThuHocKy++;
                            lsNamThuHocKy.Add(key);
                        }
                    }
                    maxRow = (int)Math.Ceiling(((decimal)ctrlChuongTrinh.TableChiTiet.Rows.Count + (decimal)countNamThuHocKy) / 2);

                }
                Dictionary<int, DSBangDiemToanKhoa.tblMonHocRow> dicRows = new Dictionary<int, DSBangDiemToanKhoa.tblMonHocRow>();
                int rowIndex = 0;
                int index = 0;
                foreach (DataRow rChiTiet in ctrlChuongTrinh.TableChiTiet.Rows)
                {
                    DSBangDiemToanKhoa.tblMonHocRow rMonHoc;
                    string col;
                    string key = rChiTiet["NamThu"].ToString() + "_" + rChiTiet["HocKy_ID"].ToString();
                    if (keyNamThuHocKy != key)
                    {
                        keyNamThuHocKy = keyNamThuHocKy;
                        int HocKy_Id = controller.ConvertInt(rChiTiet["HocKy_ID"]);
                        int NamThu = controller.ConvertInt(rChiTiet["NamThu"]);
                        if (HocKy_Id > 0 || NamThu > 0)
                        {
                            rowIndex++;
                            index = rowIndex % maxRow;
                            col = "2";
                            if (!dicRows.ContainsKey(index))
                            {
                                col = "1";
                                rMonHoc = ds.tblMonHoc.NewtblMonHocRow();
                                ds.tblMonHoc.Rows.Add(rMonHoc);
                                dicRows.Add(index, rMonHoc);
                            }
                            else
                            {
                                rMonHoc = dicRows[index];
                            }
                            rMonHoc.STT1 = rowIndex.ToString();
                            rMonHoc.TENMONHOC1 = "";
                            if (NamThu > 0)
                                rMonHoc["TENMONHOC" + col] = "Năm thứ " + rChiTiet["NamThu"].ToString();
                            if (HocKy_Id > 0)
                            {
                                if (rMonHoc["TENMONHOC" + col].ToString().Length > 0)
                                {
                                    rMonHoc["TENMONHOC" + col] = rMonHoc["TENMONHOC" + col].ToString() + "-";
                                }
                                rMonHoc["TENMONHOC" + col] = rMonHoc["TENMONHOC" + col].ToString() + "Học kỳ " + hocKyController.FindHocKy(HocKy_Id).MaHocKy;
                            }
                        }
                    }
                    rowIndex++;
                    index = rowIndex % maxRow;
                    col = "2";
                    if (!dicRows.ContainsKey(index))
                    {
                        col = "1";
                        rMonHoc = ds.tblMonHoc.NewtblMonHocRow();
                        ds.tblMonHoc.Rows.Add(rMonHoc);
                        dicRows.Add(index, rMonHoc);
                    }
                    else
                    {
                        rMonHoc = dicRows[index];
                    }
                    rMonHoc["STT" + col] = rowIndex;
                    rMonHoc["TENMONHOC" + col] = rChiTiet["TenMonHoc"].ToString();
                    rMonHoc["SOHOCTRINH" + col] = rChiTiet["SoHocPhan"].ToString();
                    DataRow[] rs = controller.Table.Select("SinhVien_ID=" + r["SINHVIEN_ID"].ToString() + " AND MonHoc_ID=" + rChiTiet["MonHoc_ID"].ToString() + " AND LanThi=1");
                    if (rs.Length > 0)
                    {
                        rMonHoc["LAN" + col + "1"] = controller.ConvertNumber(rs[0]["Diem"]).ToString("{0:N2}");
                    }
                    rs = controller.Table.Select("SinhVien_ID=" + r["SINHVIEN_ID"].ToString() + " AND MonHoc_ID=" + rChiTiet["MonHoc_ID"].ToString() + " AND LanThi=2");
                    if (rs.Length > 0)
                    {
                        rMonHoc["LAN" + col + "2"] = controller.ConvertNumber(rs[0]["Diem"]).ToString("{0:N2}");
                    }
                }
            }
            CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            doc.Load(SystemControllers.ReportHelper.GetReportPath("17_BangDiemToanKhoa.rpt"));
            doc.SetDataSource(ds);
            crystalReportViewer1.ReportSource = doc;
        }
    }
}
